﻿Imports System.Data.SqlClient

Public Class clsSalesOrderData

    Public Function SalesOrderData(ByVal sOutlet As String, ByVal sCustName As String, ByVal sContactPerName As String, _
                                   ByVal dtEventDate As Date, ByVal sConnectionString As String) As DataSet

        Dim oDataSet As DataSet
        Dim oDataAdapter As SqlDataAdapter
        Dim oDataTable As DataTable
        Dim sQuery As String = String.Empty
        Dim sFuncName As String = String.Empty
        'Reading the Header Values Based on Condition
        Try
            sFuncName = "SalesOrderData()"
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Starting Function", sFuncName)

            If sContactPerName.ToString.Trim() IsNot Nothing AndAlso sContactPerName.ToString.Trim() Then
                sContactPerName = "%"
            Else
                sContactPerName = sContactPerName & "%"
            End If
            If sCustName.ToString.Trim() IsNot Nothing AndAlso sCustName.ToString.Trim() Then
                sCustName = "%"
            Else
                sCustName = sCustName & "%"
            End If

            oDataSet = New DataSet

            sQuery = ""
            sQuery = "EXEC AE_SPES02_SalesOrderHeader '" & sOutlet & "','" & sCustName & "','" & sContactPerName & "','" & dtEventDate & "'"


            oDataAdapter = New SqlDataAdapter(sQuery, sConnectionString)
            oDataTable = New DataTable
            oDataAdapter.Fill(oDataTable)
            oDataSet.Tables.Add(oDataTable)
            If oDataSet.Tables(0).Rows.Count > 0 Then


                sQuery = ""
                sQuery = "EXEC AE_SPES03_SalesOrderLine '" & sOutlet & "','" & sCustName & "','" & sContactPerName & "','" & dtEventDate & "'"


                oDataAdapter = New SqlDataAdapter(sQuery, sConnectionString)
                oDataTable = New DataTable
                oDataAdapter.Fill(oDataTable)
                oDataSet.Tables.Add(oDataTable)

                If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with SUCCESS", sFuncName)
                Return oDataSet
            Else
                If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with SUCCESS", sFuncName)
                Return Nothing
            End If
        Catch ex As Exception
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with ERROR", sFuncName)
            WriteToLogFile(ex.Message, sFuncName)
            Return Nothing
        End Try

    End Function


    Public Function Update_SalesOrder(ByRef oDataSet As DataSet, ByRef oDICompany As SAPbobsCOM.Company, ByRef sErrDesc As String) As Long

        Dim sFuncName As String = String.Empty
        Dim oSalesOrder As SAPbobsCOM.Documents
        Dim sDocEntry As String = String.Empty

        Dim oDTDistinctHeader As DataTable = Nothing
        Dim oDTDistinctLine As DataTable = Nothing

        Dim oDataViewHeader As DataView = Nothing
        Dim oDataViewLine As DataView = Nothing

        Dim lRetCode As Long
        Dim LineNum As Int32 = 0

        Try
            sFuncName = "Update_SalesOrder()"
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Starting Function", sFuncName)

            oSalesOrder = oDICompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oOrders)

            oDataViewHeader = oDataSet.Tables(0).DefaultView
            oDataViewLine = oDataSet.Tables(1).DefaultView

            oDTDistinctHeader = oDataViewHeader.Table.DefaultView.ToTable(True, "OrderNumber")

            For IntHeader As Integer = 0 To oDTDistinctHeader.Rows.Count - 1

                sDocEntry = oDTDistinctHeader.Rows(IntHeader).Item(0).ToString

                If oSalesOrder.GetByKey(sDocEntry) Then

                    oDataViewLine.RowFilter = "F1 = '" & sDocEntry & "'"

                    'For Deleting the Row which is in already exists
                    For Row As Integer = oSalesOrder.Lines.Count To 0 Step -1
                        oSalesOrder.Lines.Delete()
                    Next

                    'Add the New Line items in an existing SO

                    LineNum = oSalesOrder.Lines.Count

                    For IntLine As Integer = 0 To oDataViewLine.Count - 1

                        oSalesOrder.Lines.SetCurrentLine(IntLine)

                        oSalesOrder.Lines.ItemCode = oDataViewLine.Item(IntLine).Row(1).ToString
                        oSalesOrder.Lines.Quantity = oDataViewLine.Item(IntLine).Row(2).ToString
                        oSalesOrder.Lines.UnitPrice = oDataViewLine.Item(IntLine).Row(3).ToString
                        oSalesOrder.Lines.WarehouseCode = oDataViewLine.Item(IntLine).Row(4).ToString

                        oSalesOrder.Lines.Add()

                    Next

                    If (oDataViewLine.Count - LineNum) < 0 Then

                        For Row As Integer = LineNum To oDataViewLine.Count Step -1
                            oSalesOrder.Lines.Delete()
                        Next

                    End If


                    lRetCode = oSalesOrder.Update()


                    If lRetCode <> 0 Then

                        If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with ERROR - DocEntry : " & oDTDistinctHeader.Rows(IntHeader).Item(0).ToString, sFuncName)
                        Return RTN_ERROR
                    Else
                        If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with SUCCESS - DocEntry : " & oDTDistinctHeader.Rows(IntHeader).Item(0).ToString, sFuncName)
                    End If
                End If

            Next

            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with SUCCESS", sFuncName)
            Return RTN_SUCCESS

        Catch ex As Exception
            Call WriteToLogFile(ex.Message, sFuncName)
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with ERROR", sFuncName)
            Return RTN_ERROR
        End Try
    End Function

End Class
